home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.AbstractAction;
- import com.sun.java.swing.CellRendererPane;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JList;
- import com.sun.java.swing.KeyStroke;
- import com.sun.java.swing.ListCellRenderer;
- import com.sun.java.swing.ListModel;
- import com.sun.java.swing.ListSelectionModel;
- import com.sun.java.swing.LookAndFeel;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.event.ListDataListener;
- import com.sun.java.swing.event.ListSelectionListener;
- import com.sun.java.swing.plaf.ComponentUI;
- import com.sun.java.swing.plaf.ListUI;
- import com.sun.java.swing.plaf.UIResource;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.LayoutManager;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.beans.PropertyChangeListener;
- import java.io.Serializable;
-
- public class BasicListUI extends ListUI implements Serializable {
- protected transient boolean hasFocus = false;
- protected ListSelectionListener selectionL;
- protected ListDataListener dataL;
- protected PropertyChangeListener propertyL;
- protected InputListener inputL;
- protected JList list = null;
- protected CellRendererPane rendererPane;
- protected int[] cellHeights = null;
- protected int cellHeight = -1;
- protected int cellWidth = -1;
- protected int updateLayoutStateNeeded = 1;
- protected static final int modelChanged = 1;
- protected static final int selectionModelChanged = 2;
- protected static final int fontChanged = 4;
- protected static final int fixedCellWidthChanged = 8;
- protected static final int fixedCellHeightChanged = 16;
- protected static final int prototypeCellValueChanged = 32;
- protected static final int cellRendererChanged = 64;
-
- protected void addListListeners() {
- this.inputL = this.createInputListener();
- this.list.addMouseListener(this.inputL);
- this.list.addMouseMotionListener(this.inputL);
- this.propertyL = this.createPropertyListener();
- this.list.addPropertyChangeListener(this.propertyL);
- this.list.addFocusListener(this.inputL);
- this.dataL = this.createDataListener();
- ListModel model = this.list.getModel();
- if (model != null) {
- model.addListDataListener(this.dataL);
- }
-
- this.selectionL = this.createSelectionListener();
- ListSelectionModel selectionModel = this.list.getSelectionModel();
- if (selectionModel != null) {
- selectionModel.addListSelectionListener(this.selectionL);
- }
-
- }
-
- protected void configureList() {
- this.list.setLayout((LayoutManager)null);
- LookAndFeel.installBorder(this.list, "List.border");
- LookAndFeel.installColorsAndFont(this.list, "List.background", "List.foreground", "List.font");
- if (this.list.getCellRenderer() == null) {
- this.list.setCellRenderer((ListCellRenderer)UIManager.get("List.cellRenderer"));
- }
-
- Color sbg = this.list.getSelectionBackground();
- if (sbg == null || sbg instanceof UIResource) {
- this.list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
- }
-
- Color sfg = this.list.getSelectionForeground();
- if (sfg == null || sfg instanceof UIResource) {
- this.list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
- }
-
- this.rendererPane = new CellRendererPane();
- this.list.add(this.rendererPane);
- }
-
- protected int convertRowToY(int row) {
- int nrows = this.list.getModel().getSize();
- Insets insets = this.list.getInsets();
- if (row >= 0 && row <= nrows) {
- if (this.cellHeights == null) {
- return insets.top + this.cellHeight * row;
- } else {
- int y = insets.top;
-
- for(int i = 0; i < row; ++i) {
- y += this.cellHeights[i];
- }
-
- return y;
- }
- } else {
- return -1;
- }
- }
-
- protected int convertYToRow(int y0) {
- int nrows = this.list.getModel().getSize();
- Insets insets = this.list.getInsets();
- if (this.cellHeights == null) {
- int row = this.cellHeight == 0 ? 0 : (y0 - insets.top) / this.cellHeight;
- return row >= 0 && row < nrows ? row : -1;
- } else {
- int y = insets.top;
- int row = 0;
-
- for(int i = 0; i < nrows; ++i) {
- if (y0 >= y && y0 < y + this.cellHeights[i]) {
- return row;
- }
-
- y += this.cellHeights[i];
- ++row;
- }
-
- return -1;
- }
- }
-
- protected ListDataListener createDataListener() {
- return new DataListener(this);
- }
-
- protected InputListener createInputListener() {
- return new InputListener(this);
- }
-
- protected PropertyChangeListener createPropertyListener() {
- return new PropertyListener(this);
- }
-
- protected ListSelectionListener createSelectionListener() {
- return new SelectionListener(this);
- }
-
- public static ComponentUI createUI(JComponent list) {
- return new BasicListUI();
- }
-
- public Rectangle getCellBounds(JList list, int index1, int index2) {
- this.maybeUpdateLayoutState();
- int minIndex = Math.min(index1, index2);
- int maxIndex = Math.max(index1, index2);
- int minY = this.convertRowToY(minIndex);
- int maxY = this.convertRowToY(maxIndex);
- if (minY != -1 && maxY != -1) {
- Insets insets = ((JComponent)list).getInsets();
- int x = insets.left;
- int w = ((JComponent)list).getWidth() - (insets.left + insets.right);
- int h = maxY + this.getRowHeight(maxIndex) - minY;
- return new Rectangle(x, minY, w, h);
- } else {
- return null;
- }
- }
-
- public Dimension getMaximumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getMinimumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getPreferredSize(JComponent c) {
- this.maybeUpdateLayoutState();
- int lastRow = this.list.getModel().getSize() - 1;
- if (lastRow < 0) {
- return new Dimension(0, 0);
- } else {
- Insets insets = this.list.getInsets();
- int width = this.cellWidth + insets.left + insets.right;
- int height = this.convertRowToY(lastRow) + this.getRowHeight(lastRow) + insets.bottom;
- return new Dimension(width, height);
- }
- }
-
- protected int getRowHeight(int row) {
- if (row >= 0 && row < this.list.getModel().getSize()) {
- return this.cellHeights == null ? this.cellHeight : (row < this.cellHeights.length ? this.cellHeights[row] : -1);
- } else {
- return -1;
- }
- }
-
- public Point indexToLocation(JList list, int index) {
- this.maybeUpdateLayoutState();
- return new Point(0, this.convertRowToY(index));
- }
-
- public void installUI(JComponent list) {
- this.list = (JList)list;
- this.configureList();
- this.addListListeners();
- this.registerKeyboardActions();
- }
-
- public int locationToIndex(JList list, Point location) {
- this.maybeUpdateLayoutState();
- return this.convertYToRow(location.y);
- }
-
- protected void maybeUpdateLayoutState() {
- if (this.updateLayoutStateNeeded != 0) {
- this.updateLayoutState();
- this.updateLayoutStateNeeded = 0;
- }
-
- }
-
- public void paint(Graphics g, JComponent c) {
- this.maybeUpdateLayoutState();
- ListCellRenderer renderer = this.list.getCellRenderer();
- ListModel dataModel = this.list.getModel();
- ListSelectionModel selModel = this.list.getSelectionModel();
- if (renderer != null && dataModel.getSize() != 0) {
- Rectangle paintBounds = g.getClipBounds();
- int firstPaintRow = this.convertYToRow(paintBounds.y);
- int lastPaintRow = this.convertYToRow(paintBounds.y + paintBounds.height - 1);
- if (firstPaintRow == -1) {
- firstPaintRow = 0;
- }
-
- if (lastPaintRow == -1) {
- lastPaintRow = dataModel.getSize() - 1;
- }
-
- Rectangle rowBounds = this.getCellBounds(this.list, firstPaintRow, firstPaintRow);
- if (rowBounds != null) {
- int leadIndex = this.list.getLeadSelectionIndex();
-
- for(int row = firstPaintRow; row <= lastPaintRow; ++row) {
- rowBounds.height = this.getRowHeight(row);
- g.setClip(rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height);
- g.clipRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
- this.paintCell(g, row, rowBounds, renderer, dataModel, selModel, leadIndex);
- rowBounds.y += rowBounds.height;
- }
-
- }
- }
- }
-
- protected void paintCell(Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer, ListModel dataModel, ListSelectionModel selModel, int leadIndex) {
- Object value = dataModel.getElementAt(row);
- boolean cellHasFocus = this.hasFocus && row == leadIndex;
- boolean isSelected = selModel.isSelectedIndex(row);
- Component rendererComponent = cellRenderer.getListCellRendererComponent(this.list, value, row, isSelected, cellHasFocus);
- int cx = rowBounds.x;
- int cy = rowBounds.y;
- int cw = rowBounds.width;
- int ch = rowBounds.height;
- this.rendererPane.paintComponent(g, rendererComponent, this.list, cx, cy, cw, ch, true);
- }
-
- protected void registerKeyboardActions() {
- AbstractAction action = new 1(this, "SelectPreviousRow");
- KeyStroke key = KeyStroke.getKeyStroke(38, 0);
- this.list.registerKeyboardAction(action, key, 0);
- action = new 2(this, "SelectNextRow");
- key = KeyStroke.getKeyStroke(40, 0);
- this.list.registerKeyboardAction(action, key, 0);
- }
-
- protected void removeListListeners() {
- this.list.removeMouseListener(this.inputL);
- this.list.removeMouseMotionListener(this.inputL);
- this.list.removePropertyChangeListener(this.propertyL);
- this.list.removeFocusListener(this.inputL);
- ListModel model = this.list.getModel();
- if (model != null) {
- model.removeListDataListener(this.dataL);
- }
-
- ListSelectionModel selectionModel = this.list.getSelectionModel();
- if (selectionModel != null) {
- selectionModel.removeListSelectionListener(this.selectionL);
- }
-
- }
-
- protected void selectNextIndex() {
- int s = this.list.getSelectedIndex();
- if (s + 1 < this.list.getModel().getSize()) {
- ++s;
- this.list.setSelectedIndex(s);
- this.list.ensureIndexIsVisible(s);
- }
-
- }
-
- protected void selectPreviousIndex() {
- int s = this.list.getSelectedIndex();
- if (s > 0) {
- --s;
- this.list.setSelectedIndex(s);
- this.list.ensureIndexIsVisible(s);
- }
-
- }
-
- protected void unconfigureList() {
- this.list.remove(this.rendererPane);
- this.rendererPane = null;
- if (this.list.getCellRenderer() instanceof UIResource) {
- this.list.setCellRenderer((ListCellRenderer)null);
- }
-
- }
-
- public void uninstallUI(JComponent c) {
- this.removeListListeners();
- this.unregisterKeyboardActions();
- this.unconfigureList();
- this.cellWidth = this.cellHeight = -1;
- this.cellHeights = null;
- this.list = null;
- }
-
- protected void unregisterKeyboardActions() {
- this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(38, 0));
- this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(40, 0));
- }
-
- protected void updateLayoutState() {
- int fixedCellHeight = this.list.getFixedCellHeight();
- int fixedCellWidth = this.list.getFixedCellWidth();
- this.cellWidth = fixedCellWidth != -1 ? fixedCellWidth : -1;
- if (fixedCellHeight != -1) {
- this.cellHeight = fixedCellHeight;
- this.cellHeights = null;
- } else {
- this.cellHeight = -1;
- this.cellHeights = new int[this.list.getModel().getSize()];
- }
-
- if (fixedCellWidth == -1 || fixedCellHeight == -1) {
- ListModel dataModel = this.list.getModel();
- int dataModelSize = dataModel.getSize();
- ListCellRenderer renderer = this.list.getCellRenderer();
- if (renderer != null) {
- for(int index = 0; index < dataModelSize; ++index) {
- Object value = dataModel.getElementAt(index);
- Component c = renderer.getListCellRendererComponent(this.list, value, index, false, false);
- this.rendererPane.add(c);
- Dimension cellSize = c.getPreferredSize();
- if (fixedCellWidth == -1) {
- this.cellWidth = Math.max(cellSize.width, this.cellWidth);
- }
-
- if (fixedCellHeight == -1) {
- this.cellHeights[index] = cellSize.height;
- }
- }
- } else {
- if (this.cellWidth == -1) {
- this.cellWidth = 0;
- }
-
- for(int index = 0; index < dataModelSize; ++index) {
- this.cellHeights[index] = 0;
- }
- }
- }
-
- this.list.invalidate();
- }
- }
-